Ext.ns('app.crud.news');

app.crud.news.canEdit = false;
app.crud.news.canPublish = false;
app.crud.news.canDelete = false;

Ext.define('app.crud.news.ListModel', {
    extend: 'Ext.data.Model',
    fields: [
     	{name:'id' , type:'integer'},
        {name:'title' , type:'string'},
        {name:'news_date', type:'date' , dateFormat:'Y-m-d'},	 	        
        {name:'date_created', type:'date' , dateFormat:'Y-m-d H:i:s'},
        {name:'date_updated',type:'date' , dateFormat:'Y-m-d H:i:s'},
        {name:'user',type:'string' },
        {name:'updater',type:'string'},
        {name:'published' , type:'boolean'},
        {name:'published_version' , type:'integer'},
        {name:'last_version' , type:'integer'}
    ]
});

Ext.define('app.crud.news.Main',{
	  extend:'Ext.panel.Panel',
	  /**
	    * @property {Ext.grid.Panel}
	    */
	   dataGrid:null,
	   /**
	    * @property {Ext.data.Store}
	    */
	   dataStore:null,
	   /**
	    * @property {searchBar}
	    */
	   searchField: null,
	   /**
	    * @property {Ext.Button}
	    */
	   addItemBtn: null,
	   
	   constructor: function(config) {
			
			config = Ext.apply({
				layout:'fit'
		    }, config);		
			
			this.callParent(arguments);

			this.dataStore = Ext.create('Ext.data.Store', {
			    model: 'app.crud.news.ListModel',
			    proxy: {
			        type: 'ajax',
			    	url:app.root +  'list',
			        reader: {
			            type: 'json',
			            root: 'data',
			            totalProperty: 'count',
			            idProperty: 'id'
			        },
			        startParam:'pager[start]',
			        limitParam:'pager[limit]',
			        sortParam:'pager[sort]',
			        directionParam:'pager[dir]',
				    simpleSortMode: true
			    },
			    pageSize: 50,
		        remoteSort: true,
			    autoLoad: true,
			    sorters: [{
	                  property : 'date_updated',
	                  direction: 'DESC'
	            }]
			});
			
			this.filterStore = Ext.create('Ext.data.Store', {
				autoLoad:true,
				model:'app.comboModel',
				proxy: {
			        type: 'ajax',
			        url:app.root + 'yearlist',
			        reader: {
			            type: 'json',
			            root: 'data',
			            idProperty: 'id'
			        }
			    }
			});
			
			this.yearFilter =  new Ext.form.field.ComboBox({
				displayField:"text",
				queryMode: 'local',
				emptyText:'Select year',
				forceSelection:true,
				triggerAction:"all",
				valueField:"id",
				displayField:'title',
				valueField:'id',
				value:'All',
				width:90,
				store:this.filterStore 
			});
			
			this.yearFilter.on('select' , function(combo, record,  index ){
					this.dataStore.proxy.extraParams['filter[year]'] = combo.getValue();
					this.dataStore.load();
			} , this);

		    this.searchField =  new SearchPanel({store:this.dataStore,local:false});
	
			this.addItemBtn = new Ext.Button({
				 text:'Add Item',
				 hidden:(!app.crud.news.canEdit )
			});
			
			this.addItemBtn.on('click' , function(){
				 this.showPageEdit(0);
			} , this);
			
			
			this.dataGrid = Ext.create('Ext.grid.Panel',{
					store: this.dataStore,
				 	viewConfig:{
				 		stripeRows:false
				 	},
		            frame: false,
		            loadMask:true,
				    columnLines: true,
				    autoScroll:true,
				    tbar:[
				          'Year' ,'-' ,this.yearFilter ,'-',this.addItemBtn , '->'  , this.searchField
				    ],
				    columns: [
							{
								id: 'published',
							    sortable: true,
							    text: 'Status',
							    dataIndex: 'published',
							    width:50,
							    align:'center',
							    renderer:app.publishRenderer 
							}, {
								text:'Versions (Published / Last)',
								dataIndex:'id',
								align:'center',
								width:150,
								renderer:app.versionRenderer
				             }, {
					            sortable: true,
					            text: 'On Homepage',
					            dataIndex: 'show_on_home_page',
					            align:'center',
					            width:80,
					            renderer:app.checkboxRenderer 
					        },{
							    sortable: true,
							    align:'center',
							    text: 'Date',
							    dataIndex: 'news_date',
							    xtype:'datecolumn',
							    format:'M d, Y'
							},{
								id: 'title',
							    sortable: true,
							    text: 'Headline',
							    dataIndex: 'title',
							    flex:1
							},{
					        	text:'Created by',
					        	dataIndex:'date',
					        	sortable: true,
					        	width:210,
					        	renderer:app.creatorRenderer
					          },{
					        	text:'Updated By',
					        	dataIndex:'date_updated',
					        	sortable: true,
						        width:210,
						        renderer:app.updaterRenderer
					          }
				    ],
					bbar: Ext.create('Ext.PagingToolbar', {
			            store: this.dataStore,
			            displayInfo: true,
			            displayMsg: 'Displaying records {0} - {1} of {2}',
			            emptyMsg: "No records to display"
			        }),
					listeners : {
				    	'itemdblclick':{
				    		fn:function(view , record , number , event , options){
				    			 this.showPageEdit(record.get('id'));
				    		},
				    		scope:this
				    	}
					}
			});
			
			this.add(this.dataGrid);
	   },
	   showPageEdit: function(id){
			var win = Ext.create('app.crud.news.Window', {
				dataItemId:id,
				canDelete:app.crud.news.canDelete,
				canEdit:app.crud.news.canEdit,
				canPublish:app.crud.news.canPublish
			});
			
			win.on('dataSaved' , function(){
				this.dataStore.load();
				this.filterStore.load();
			}, this);		
			
			win.show();
			
			if(id){
				win.loadData(id , false);
			}	
		}
});

Ext.define('app.crud.news.Window',{
	  extend:'app.contentWindow',
	  textPanel:null,
	  constructor: function(config) {		
			config = Ext.apply({
		        title: 'News :: Edit Item',
		        width: 720,
		        height:640,
		        objectName:'news',
		        controllerUrl:app.root
		    }, config);		
				
			this.textPanel = Ext.create('app.medialib.HtmlPanel',{
		    	title:'Text',
		    	editorName:'text',
		    	listeners:{
		    		activate:{
		    			fn:function(){
		    				
		    			},
		    			scope:this
		    		}
		    	}
			});
			
			this.callParent(arguments);
			
			this.contentTabs.getComponent(0).add([
			    this.textPanel,
				{
					fieldLabel:"Headline",
					allowBlank:false,
					name:"title",
					xtype:"textfield"
				},{
					fieldLabel:'Date',
					allowBlank:false,
					xtype:'datefield',
					name:'news_date',
					anchor:false,
					width: 250,
					format:'Y-m-d'
				}
			 ]);
				
			this.contentTabs.add(this.textPanel);
	   }
});

Ext.onReady(function(){ 

	app.crud.news.canEdit = canEdit;
	app.crud.news.canPublish = canPublish;
	app.crud.news.canDelete = canDelete;
	var dataPanel = Ext.create('app.crud.news.Main',{
		title:appLang.MODULE_NEWS + ' :: ' + appLang.HOME
	});
	
	app.content.add(dataPanel);
});